import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '@/views/Main'
import CategoryCreate from '@/views/CategoryCreate'
import CategoryList from '@/views/CategoryList'
import ItemEdit from '@/views/ItemEdit'
import ItemList from '@/views/ItemList'
import HeroEdit from '@/views/HeroEdit'
import HeroList from '@/views/HeroList'

import AdminUserEdit from '@/views/AdminUserEdit'
import AdminUserList from '@/views/AdminUserList'
import Login from '@/views/Login'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Main,
    children: [
      {
        path: '/categories/edit',
        component: CategoryCreate,
        name: 'CrategoryEdit',
      }, {
        path: '/categories/list',
        component: CategoryList,
        name: 'CategoryList'
      }, {
        path: '/categories/edit/:id',
        component: CategoryCreate,
        props: true
      }, 
      
      
      {
        path: '/items/edit',
        component: ItemEdit,
        name: 'ItemEdit',
      }, {
        path: '/items/list',
        component: ItemList,
        name: 'ItemList'
      }, {
        path: '/items/edit/:id',
        component: ItemEdit,
        props: true
      },

      {
        path: '/heros/edit',
        component: HeroEdit,
        name: 'HeroEdit',
      }, {
        path: '/heros/list',
        component: HeroList,
        name: 'HeroList'
      }, {
        path: '/heros/edit/:id',
        component: HeroEdit,
        props: true
      },

      {
        path: '/admin_users/edit',
        component: AdminUserEdit,
        name: 'AdminUserEdit',
      }, {
        path: '/admin_users/list',
        component: AdminUserList,
        name: 'AdminUserList'
      }, {
        path: '/admin_users/edit/:id',
        component: AdminUserEdit,
        props: true
      },

    ]
  },{
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      isPublic: true
    }
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach( (to,from,next)=>{

  if(!to.meta.isPublic && !localStorage.token){
    next('/login')
  } else {
    next()
  }
  
})

export default router
